﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using CommunityServer.WebServices.MediaGalleries;
using System.Threading;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace nb.development.csmediamanager
{
    public partial class ShellAdd : Form
    {
        public ShellAdd(DirectoryInfo dir)
        {
            InitializeComponent();
            this.files = new List<FileInfo>();
            foreach (FileInfo file in dir.GetFiles("*", SearchOption.TopDirectoryOnly))
                if (!file.Extension.ToLower().Equals(".ini") && !file.Extension.ToLower().Equals(".inf") && !file.Extension.ToLower().Equals(".db"))
                    this.files.Add(file);
            LoadGalleries();
        }

        public ShellAdd(FileInfo file)
        {
            InitializeComponent();
            this.files = new List<FileInfo>();
            files.Add(file);
            LoadGalleries();
        }

        private List<FileInfo> files;

        private void LoadGalleries()
        {
            foreach (FileInfo file in files)
            {
                TreeNode node = new TreeNode(file.Name);
                node.Checked = true;
                fileList.Nodes.Add(node);
            }
            foreach (Site s in Sites.Instance.Values)
            {
                TreeNode node = new TreeNode(s.Name);
                node.Name = s.Id.ToString();
                node.Nodes.Add("Loading...");
                galleryList.Nodes.Add(node);
                GalleryWorker gw = new GalleryWorker(s.Id);
                gw.Completed += new GalleryWorkerCompleted(gw_Completed);
                Thread t = new Thread(new ThreadStart(gw.Run));
                t.Start();
            }
        }

        void gw_Completed(Guid siteid, Dictionary<int, MediaGallery> galleries, Dictionary<int, MediaGalleryGroup> groups)
        {
            if (galleryList.InvokeRequired)
            {
                GalleryWorkerCompleted done = new GalleryWorkerCompleted(gw_Completed);
                try { galleryList.Invoke(done, siteid, galleries, groups); }
                catch { }
            }
            else
            {
                try
                {
                    TreeNode root = galleryList.Nodes[siteid.ToString()];
                    root.Nodes.Clear();
                    foreach (MediaGallery g in galleries.Values)
                    {
                        TreeNode n = null;
                        if (root.Nodes.Find(siteid.ToString() + "\\" + g.MediaGalleryGroupId.ToString(), false).Length > 0)
                            n = root.Nodes.Find(siteid.ToString() + "\\" + g.MediaGalleryGroupId.ToString(), false)[0];
                        if (n != null) addGallery(g, n);
                        else
                        {
                            TreeNode node = new TreeNode(groups[g.MediaGalleryGroupId].Name);
                            node.ToolTipText = groups[g.MediaGalleryGroupId].Description;
                            node.Name = siteid.ToString() + "\\" + g.MediaGalleryGroupId.ToString();
                            addGallery(g, node);
                            root.Nodes.Add(node);
                        }
                    }
                }
                catch (Exception e) { MessageBox.Show("Error: " + e.Message, "Error Loading Galleries", MessageBoxButtons.OK, MessageBoxIcon.Error); }
            }
        }

        private void addGallery(MediaGallery g, TreeNode node)
        {
            TreeNode n = new TreeNode(g.Name);
            n.Name = g.Id + "";
            n.ToolTipText = g.Description;
            node.Nodes.Add(n);
            node.Expand();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            List<FileInfo> f2 = new List<FileInfo>();
            for (int x = 0; x < files.Count; x++)
                if (fileList.Nodes[x].Checked)
                    f2.Add(files[x]);
            Uploader u = new Uploader(f2, new Guid(galleryList.SelectedNode.Parent.Parent.Name), int.Parse(galleryList.SelectedNode.Name), tags.Text);
            progress.Maximum = f2.Count;
            u.UploadedPost += new UploadedPost(u_UploadedPost);
            u.UploadCompleded += new UploadCompleted(u_UploadCompleded);
            progress.Value = 0;
            btnOK.Enabled = false;
            btnCancel.Enabled = false;
            fileList.Enabled = false;
            tags.Enabled = false;
            ControlBox = false;
            Cursor = Cursors.WaitCursor;
            Thread t = new Thread(new ThreadStart(u.UploadDirectory));
            t.Start();

        }

        void u_UploadCompleded()
        {
            if (this.InvokeRequired)
            {
                UploadCompleted done = new UploadCompleted(u_UploadCompleded);
                try { this.Invoke(done); }
                catch { }
            }
            else
            {
                this.DialogResult = DialogResult.OK;
                MessageBox.Show("The Selected file(s) have been upload.", "Upload Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close(); Application.Exit();
            }
        }

        void u_UploadedPost(int index, int count)
        {
            if (progress.InvokeRequired)
            {
                UploadedPost done = new UploadedPost(u_UploadedPost);
                try { this.Invoke(done, index, count); }
                catch { }
            }
            else progress.Value = index;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close(); Application.Exit();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            galleryList.Show();
        }

        private void galleries_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Parent != null)
            {
                if (e.Node.Parent.Parent != null)
                {
                    galleryList.Hide();
                    btnSelect.Text = e.Node.Parent.Text + " \\ " + e.Node.Text;
                    btnOK.Enabled = true;
                }
                else btnOK.Enabled = false;
            }
            else btnOK.Enabled = false;
        }
    }
}
